// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "WinstonLogger.h"
#include "ProceduralTerrain.generated.h"

/**
 * 程序化地形生成组件
 * 用于生成和管理游戏中的地形
 */
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class EMPTYLAND_API UProceduralTerrain : public UActorComponent
{
    GENERATED_BODY()

public:
    // 构造函数
    UProceduralTerrain();

protected:
    // 初始化函数
    virtual void BeginPlay() override;

public:
    // 更新函数
    virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

    /**
     * 生成程序化地形
     * @param Seed 随机种子，用于生成一致性地形
     * @param NoiseScale 噪声缩放因子
     * @param Octaves 噪声八度
     * @param Persistence 噪声持久性
     * @param Lacunarity 噪声间隙度
     */
    UFUNCTION(BlueprintCallable, Category = "Terrain")
    void GenerateProceduralTerrain(int32 Seed = 12345, float NoiseScale = 100.0f, int32 Octaves = 4, float Persistence = 0.5f, float Lacunarity = 2.0f);

    /**
     * 获取高度图数据
     * @param X 位置X
     * @param Y 位置Y
     * @return 该位置的高度值
     */
    float GetHeightAt(float X, float Y);

private:
    // 噪声种子
    UPROPERTY(EditDefaultsOnly, Category = "Terrain Settings", meta = (AllowPrivateAccess = "true"))
    int32 NoiseSeed;

    // 噪声缩放
    UPROPERTY(EditDefaultsOnly, Category = "Terrain Settings", meta = (AllowPrivateAccess = "true"))
    float NoiseScale;

    // 噪声八度
    UPROPERTY(EditDefaultsOnly, Category = "Terrain Settings", meta = (AllowPrivateAccess = "true"))
    int32 Octaves;

    // 噪声持久性
    UPROPERTY(EditDefaultsOnly, Category = "Terrain Settings", meta = (AllowPrivateAccess = "true"))
    float Persistence;

    // 噪声间隙度
    UPROPERTY(EditDefaultsOnly, Category = "Terrain Settings", meta = (AllowPrivateAccess = "true"))
    float Lacunarity;

    // 最大高度
    UPROPERTY(EditDefaultsOnly, Category = "Terrain Settings", meta = (AllowPrivateAccess = "true"))
    float MaxHeight;

    // 最小高度
    UPROPERTY(EditDefaultsOnly, Category = "Terrain Settings", meta = (AllowPrivateAccess = "true"))
    float MinHeight;

    /**
     * 生成单样本噪声值
     * @param X 位置X
     * @param Y 位置Y
     * @return 噪声值 (-1.0f 到 1.0f)
     */
    float GenerateNoise(float X, float Y);

    /**
     * 生成分形噪声（Perlin噪声变体）
     * @param X 位置X
     * @param Y 位置Y
     * @return 分形噪声值
     */
    float GenerateFractalNoise(float X, float Y);

    /**
     * 线性插值
     * @param A 起始值
     * @param B 结束值
     * @param T 插值因子 (0.0f 到 1.0f)
     * @return 插值结果
     */
    float Lerp(float A, float B, float T);

    /**
     * 平滑插值
     * @param A 起始值
     * @param B 结束值
     * @param T 插值因子 (0.0f 到 1.0f)
     * @return 平滑插值结果
     */
    float Smoothstep(float A, float B, float T);
};